pub mod entity;
use crate::entity::prelude::User;
use entity::user;
use sea_orm::{
    prelude::Expr, ActiveModelTrait as _, ActiveValue::Set, ColumnTrait as _, Database,
    EntityTrait as _, QueryFilter as _, Value,
};

#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
    let db = Database::connect("sqlite://user.db").await?;

    let users: Vec<user::Model> = User::find().all(&db).await?;

    let user: Vec<user::Model> = User::find()
        .filter(user::Column::Name.contains("yc"))
        .all(&db)
        .await?;

    let user: Option<user::Model> = User::find_by_id(1).one(&db).await?;
    let mut user: user::ActiveModel = user.unwrap().into();

    user.name = Set("yc".to_owned());

    // update one
    let user: user::Model = user.update(&db).await?;

    // update many: UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."name" LIKE '%Apple%'
    User::update_many()
        .col_expr(user::Column::Age, Expr::value(Value::Int(Some(1))))
        .filter(user::Column::Name.contains("Apple"))
        .exec(&db)
        .await?;

    Ok(())
}
